USE [Monitoring]
GO

--|--------------------------------------------------------------------------------
--| [GetUserByID] - Insert Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[GetUsersInTeam]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[GetUsersInTeam]
GO

CREATE PROCEDURE [dbo].[GetUsersInTeam]
(
	@TeamID int
)
AS
	SET NOCOUNT ON

	SELECT
		[ID],
		[Login],
		[Email],
		[Password],
		'' as [Roles],
		[TeamID]
	FROM 
	[User]
	WHERE
		[TeamID] = @TeamID
	
GO
--|--------------------------------------------------------------------------------
--| [GetUserByID] - Insert Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[GetUserByID]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[GetUserByID]
GO

CREATE PROCEDURE [dbo].[GetUserByID]
(
	@ID int
)
AS
	SET NOCOUNT ON

	SELECT
		[ID],
		[Login],
		[Email],
		[Password],
		[TeamID]
	FROM 
	[User]
	WHERE
		[ID] = @ID
	
GO

--|--------------------------------------------------------------------------------
--| [GetUserByLogin] - Insert Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[GetUserByLogin]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[GetUserByLogin]
GO

CREATE PROCEDURE [dbo].[GetUserByLogin]
(
	@Login nvarchar(50)
)
AS
	SET NOCOUNT ON

	SELECT
		[ID],
		[Login],
		[Email],
		[Password],
		[TeamID],
		'' as [Roles]
	FROM 
	[User]
	WHERE
		[Login] = @Login
	
GO

--|--------------------------------------------------------------------------------
--| [GetUserByLoginPassword] - Insert Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[GetUserByLoginPassword]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[GetUserByLoginPassword]
GO

CREATE PROCEDURE [dbo].[GetUserByLoginPassword]
(
	@Login nvarchar(50),
	@Password nvarchar(50)
)
AS
	SET NOCOUNT ON

	SELECT
		[ID],
		[Login],
		[Password],
		[TeamID]
	FROM 
	[User]
	WHERE
		[Login] = @Login AND [Password] = @Password
	
GO


--|--------------------------------------------------------------------------------
--| [InsertUser] - Insert Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[InsertUser]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[InsertUser]
GO

CREATE PROCEDURE [dbo].[InsertUser]
(
	@ID int = NULL OUTPUT,
	@Login nvarchar(50),
	@Password nvarchar(50),
	@Email nvarchar(50),
	@TeamID int
)
AS
	SET NOCOUNT ON

	INSERT INTO [User]
	(
		[Login],
		[Password],
		[Email],
		[TeamID]
	)
	VALUES
	(
		@Login,
		@Password,
		@Email,
		@TeamID
	)

	SELECT @ID = SCOPE_IDENTITY();

	RETURN @@Error
GO

--|--------------------------------------------------------------------------------
--| [ChangePassword] - Update Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[ChangePassword]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
	DROP PROCEDURE [dbo].[ChangePassword]
GO

CREATE PROCEDURE [dbo].[ChangePassword]
(
	@ID int,
	@Password nvarchar(50)
)
AS
	SET NOCOUNT ON
	
	UPDATE [User]
	SET
		[Password] = @Password
	WHERE 
		[ID] = @ID

	RETURN @@Error
GO

--|--------------------------------------------------------------------------------
--| [DeleteUser] - Update Procedure Script for User
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[DeleteUser]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
	DROP PROCEDURE [dbo].[DeleteUser]
GO

CREATE PROCEDURE [dbo].[DeleteUser]
(
	@ID int
)
AS
	SET NOCOUNT ON

	DELETE 
	FROM   [User]
	WHERE  
		[ID] = @ID

	RETURN @@Error
GO

